package com.da.landlord;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Index;
import org.hibernate.annotations.Where;

import com.googlecode.cswish.annotation.cm;
import com.ra.landlord.LandlordOwner;
import com.ra.landlord.LandlordOwnerUser;
import com.ra.landlord.LandlordUser;
import com.ra.landlord.constant.RoomType;
import com.ra.util.Address;

/**
 * 套间
 * @author Peter.Z
 *
 */
@Entity
@Table(name="da_dasuite")
//@cm("套间")
	
public class DaSuite implements LandlordOwner {
	
	/**
	 * 已出租状态
	 */
	public final static Integer RENTSTATE_RENTED = 0;
	
	/**
	 * 未出租状态
	 */
	public final static Integer RENTSTATE_FREE = 1;
	/**
	 * 部份出租
	 */
	public final static Integer RENTSTATE_XXX = 2;
	
	@cm("整租")
	public final static Integer RENT_TYPE_ENTIRE = 0;
	@cm("分租")
	public final static Integer RENT_TYPE_SUB = 1;
	
	private Integer id;
	
	@cm("名称")
	private String name;
	
	@cm("面积")
	private BigDecimal area;
	
	@cm("房屋朝向")
	private Integer orientation;
	
	@cm("租借状态")		// (0:已出租；1：未出租；2.合租有出租也有未出租)
	private Integer rentState;
	
	@cm("楼层")
	private Integer floorNo;
	
	@cm("楼层总数")
	private Integer floorCount;
	
	@cm("所有房间")
	private List<DaRoom> rooms;
	
	@cm("得到所有已出租的房间")
	private List<DaRoom> rentRooms;
	
	@cm("房源地址")
	private Address address;
	
	@cm("房屋出租类型 ")		// 0-整租 1-合租  null-整租
	private Integer rentType;
	
	@cm("房东")
	private LandlordOwnerUser landlordUser;
	
	@cm("业主")
	private DaOwner daOwner;
	
	@cm("租金 ")		// 元/月
	private BigDecimal rentPay;
	
	@cm("押几") //押几 实际收费按合同收费 
	private Integer depositNum;
	
	@cm("付几 ") //付几  实际收费按合同收费 交租日期=起租日期+付几月  -提前多天 
	private Integer payNum;
	
	@cm("几室")
	private Integer roomCount;
	
	@cm("几厅")
	private Integer hallCount;
	
	@cm("几卫")
	private Integer toiletCount;
	
	@cm("装修级别") //精装,简装
	private Integer decorateLevel;
	
	@cm("房源设施")
	private DaSuiteItem suiteItem;
	
	@cm("房源备注")
	private String mark;
	
	@cm("费用所有有效的规则")
	private List<DaFeeRule> feeRules;
	
	@cm("房源所对应的附件信息")
	private List<DaSuiteAttachment> attachments;
	
	//transient
	@cm("上次抄表")
	private DaMeter lastMeter;
	
	@cm("上上次抄表")
	private DaMeter lastLastMeter;
	
	// transient, 指定某一计费规则下， 该规则的本次读表数据和上次读表数据
	@cm("本次抄表")
	private DaMeter currentMeter;
	
	@cm("房源信息创建时间")
	private Date createDate;
	
	//房东管理员
	private List<LandlordUser> landlordUsers;
	
	@cm("transient 符合查询条件的room数量")
	private Integer roomSize;
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Index(name = "idx_name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Integer getRoomCount() {
		return roomCount;
	}

	public void setRoomCount(Integer roomCount) {
		this.roomCount = roomCount;
	}

	public Integer getHallCount() {
		return hallCount;
	}

	public void setHallCount(Integer hallCount) {
		this.hallCount = hallCount;
	}

	public Integer getToiletCount() {
		return toiletCount;
	}

	public void setToiletCount(Integer toiletCount) {
		this.toiletCount = toiletCount;
	}

	public BigDecimal getRentPay() {
		return rentPay;
	}

	public void setRentPay(BigDecimal rentPay) {
		this.rentPay = rentPay;
	}

	public BigDecimal getArea() {
		return area;
	}

	public void setArea(BigDecimal area) {
		this.area = area;
	}

	public Integer getOrientation() {
		return orientation;
	}

	public void setOrientation(Integer orientation) {
		this.orientation = orientation;
	}

	public Integer getRentState() {
		return rentState;
	}

	public void setRentState(Integer rentState) {
		this.rentState = rentState;
	}

	public Integer getFloorNo() {
		return floorNo;
	}

	public void setFloorNo(Integer floorNo) {
		this.floorNo = floorNo;
	}

	public Integer getFloorCount() {
		return floorCount;
	}

	public void setFloorCount(Integer floorCount) {
		this.floorCount = floorCount;
	}

	/**
	 * 得到有效的费用规则
	 * @return
	 */
	@OneToMany(mappedBy="suite" ,cascade=CascadeType.ALL)
	@Where(clause="state=1")
	public List<DaFeeRule> getFeeRules() {
		return feeRules;
	}
	
	public void setFeeRules(List<DaFeeRule> feeRules) {
		this.feeRules = feeRules;
	}
	

	public Integer getDecorateLevel() {
		return decorateLevel;
	}

	public void setDecorateLevel(Integer decorateLevel) {
		this.decorateLevel = decorateLevel;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@OneToMany(mappedBy="suite",cascade=CascadeType.ALL)
	public List<DaRoom> getRooms() {
		return rooms;
	}

	public void setRooms(List<DaRoom> rooms) {
		this.rooms = rooms;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public LandlordOwnerUser getLandlordUser() {
		return landlordUser;
	}

	@Override
	public void setLandlordUser(LandlordOwnerUser LandlordOwner) {
		this.landlordUser = LandlordOwner;
	}

	@ManyToOne(cascade=CascadeType.MERGE)
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Integer getRentType() {
		return rentType;
	}

	public void setRentType(Integer rentType) {
		this.rentType = rentType;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public DaOwner getDaOwner() {
		return daOwner;
	}

	public void setDaOwner(DaOwner daOwner) {
		this.daOwner = daOwner;
	}

	@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	public DaSuiteItem getSuiteItem() {
		return suiteItem;
	}

	public void setSuiteItem(DaSuiteItem suiteItem) {
		this.suiteItem = suiteItem;
	}

	public Integer getDepositNum() {
		return depositNum;
	}

	public void setDepositNum(Integer depositNum) {
		this.depositNum = depositNum;
	}

	public Integer getPayNum() {
		return payNum;
	}

	public void setPayNum(Integer payNum) {
		this.payNum = payNum;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}
	
	@Transient
	public DaMeter getLastMeter() {
		return lastMeter;
	}

	public void setLastMeter(DaMeter lastMeter) {
		this.lastMeter = lastMeter;
	}
	
	@Transient
	public DaMeter getLastLastMeter() {
		return lastLastMeter;
	}

	public void setLastLastMeter(DaMeter lastLastMeter) {
		this.lastLastMeter = lastLastMeter;
	}
	
	@Transient
	public DaMeter getCurrentMeter() {
		return currentMeter;
	}

	public void setCurrentMeter(DaMeter currentMeter) {
		this.currentMeter = currentMeter;
	}

	@OneToMany(mappedBy="suite")
	@Where(clause = "rentState=0")
	public List<DaRoom> getRentRooms() {
		return rentRooms;
	}

	public void setRentRooms(List<DaRoom> rentRooms) {
		this.rentRooms = rentRooms;
	}

	
	@OneToMany(mappedBy="suite" ,cascade=CascadeType.ALL, orphanRemoval=true)
	public List<DaSuiteAttachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<DaSuiteAttachment> attachments) {
		this.attachments = attachments;
	}
	
	@ManyToMany(targetEntity=LandlordUser.class)
	@JoinTable(name = "da_suite_landlorduser", 
		joinColumns = @JoinColumn(name="suite_id"),
		inverseJoinColumns = @JoinColumn(name="user_id"))
	public List<LandlordUser> getLandlordUsers()
	{
		return landlordUsers;
	}

	public void setLandlordUsers(List<LandlordUser> landlordUsers)
	{
		this.landlordUsers = landlordUsers;
	}

	@Transient
	public Integer getRoomSize() {
		return roomSize;
	}

	public void setRoomSize(Integer roomSize) {
		this.roomSize = roomSize;
	}
	
	@Transient
	public int getAdaptRoomType()
	{
		int roomType = RoomType.RTM_1R_0H;
		switch (this.roomCount) 
		{
		case 1:
			if (this.hallCount == 0)
			{
				roomType = RoomType.RTM_1R_0H;
			}
			else 
			{
				roomType = RoomType.RTM_1R_1H;
			}
			break;
		case 2:
			if (this.hallCount == 1)
			{
				roomType = RoomType.RTM_2R_1H;
			}
			else 
			{
				roomType = RoomType.RTM_2R_2H;
			}
			break;
		case 3:
			if (this.hallCount == 1)
			{
				roomType = RoomType.RTM_3R_1H;
			}
			else 
			{
				roomType = RoomType.RTM_3R_2H;
			}
			break;
	
		default:
			
		}
		
		return roomType;
	}

}
